package com.hzy.notepad;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.hzy.listview.NotepadListActivity;
import com.hzy.login.UserDBHelper;
import com.hzy.text.R;
import com.sam.widget.headerBar.LeftImgTitleBar;
import com.sam.widget.headerBar.LeftImgTitleRightImgBar;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/6/24 0024.
 */
public class NotepadActivity extends AppCompatActivity{
    private EditText noteEdit,noteTitle;
    private UserDBHelper userDBHelper;
    private String userName;
    private LeftImgTitleRightImgBar leftImgTitleRightImgBar;
    private String noteText,noteTitleText;
    private String _id;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad_edit);
        Intent intent = getIntent();
        userName = intent.getStringExtra("userName");
        noteText = intent.getStringExtra("note");
        _id = intent.getStringExtra("id");
        noteTitleText = intent.getStringExtra("noteTitle");
        findView();
        initView();
    }

    private void findView(){
        noteEdit = (EditText) findViewById(R.id.note_edit);
        noteTitle = (EditText) findViewById(R.id.note_title);
        leftImgTitleRightImgBar = (LeftImgTitleRightImgBar) findViewById(R.id.notepad_tltlebar);

    }

    private void  initView(){
        noteEdit.setText(noteText);
        noteTitle.setText(noteTitleText);

        leftImgTitleRightImgBar.setOnTitleRightClickListener(new LeftImgTitleRightImgBar.OnTitleRightClickListener() {
            @Override
            public void onRightClick() {

                ContentValues values = new ContentValues();
                values.put("noteTitle",noteTitle.getText().toString());
                values.put("note",noteEdit.getText().toString());
                values.put("userName",userName);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
                Date curDate = new Date(System.currentTimeMillis());
                String str = formatter.format(curDate);
                values.put("dateTime",str);

                if (noteTitle != null){
                    userDBHelper =new UserDBHelper(NotepadActivity.this,"User.db",null,1);
                    SQLiteDatabase db = userDBHelper.getWritableDatabase();


                    if (_id != null){
                        db.update("Note",values,"userName = ? and _id = ?",new String[]{userName,_id+""});
                    } else {
                        db.insert("Note",null,values);
                        values.clear();
                    }

                } else {
                    Toast.makeText(NotepadActivity.this,"请输入标题",Toast.LENGTH_SHORT).show();
                }

                startNotepadListActivity();
            }
        });

        leftImgTitleRightImgBar.setOnTitleLeftClickListener(new LeftImgTitleBar.OnTitleLeftClickListener() {
            @Override
            public void onLeftClick() {
              finish();
            }
        });
    }

    private void startNotepadListActivity(){
        Intent intent = new Intent(NotepadActivity.this, NotepadListActivity.class);
        intent.putExtra("userName",userName);
        startActivity(intent);
    }
}
